package com.yuyou.activity.ts.api.controller;

import com.yuyou.activity.comm.service.service.ISmsService;
import com.yuyou.activity.ts.service.entity.ActivityTsUser;
import com.yuyou.activity.ts.service.service.IActivityTsUserDetailService;
import com.yuyou.activity.ts.service.service.IActivityTsUserService;
import com.yuyou.activity.ts.service.vo.req.ReqLoginActivityTs;
import com.yuyou.activity.ts.service.vo.resp.RespExamDetail;
import com.yuyou.activity.ts.service.vo.resp.RespLoginActivityTs;
import com.yuyou.activity.ts.service.vo.resp.RespExamResult;
import com.yuyou.activity.util.bean.ResBoolSimpleInfo;
import com.yuyou.activity.util.bean.ReturnResult;
import com.yuyou.activity.util.util.Sequence;
import com.yuyou.activity.util.util.SpringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * Created by yujin.liu on 2018/12/19.
 */
@Api(value="ActivityTsController",description="答题分班接口")
@Validated
@RestController
@RequestMapping(value="/api/activityTs")
public class ActivityTsController extends BaseController{

    @Autowired
    private ISmsService smsService;

    @Autowired
    private IActivityTsUserService activityTsUserService;

    @Autowired
    private IActivityTsUserDetailService activityTsUserDetailService;

    @ApiOperation(value="登录活动")
    @RequestMapping(value="/loginActivityTs",method = {RequestMethod.POST})
    public ReturnResult<ResBoolSimpleInfo<RespLoginActivityTs>> loginActivityTs(@Validated @ModelAttribute ReqLoginActivityTs reqLoginActivityTs) throws Exception{

        this.valAllClose(reqLoginActivityTs.getProjectCode());
        ResBoolSimpleInfo<RespLoginActivityTs> resDet=new ResBoolSimpleInfo<>();
        //验证短信校验码
        ResBoolSimpleInfo<String> res = smsService.validate(reqLoginActivityTs.getPhone(), "01", reqLoginActivityTs.getCode());

        //测试环境配置
        Environment env = (Environment) SpringUtil.getBean("environment");
        String messageOpen = env.getProperty("message.open");
        Integer time = Integer.parseInt(env.getProperty(reqLoginActivityTs.getProjectCode() + "_time"));
        if(messageOpen.equals("off")){
            resDet = activityTsUserService.LoginActivityTs(reqLoginActivityTs, time);
        }else{
            if(res.isSuccess()){
                resDet = activityTsUserService.LoginActivityTs(reqLoginActivityTs, time);
            }else{
                resDet.setMessage(res.getMessage());
                resDet.setSuccess(false);
            }
        }
        return ReturnResult.returnRes(resDet);
    }

    @ApiOperation(value="更新用户活动状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "项目编号", paramType = "query", required = true),
            @ApiImplicitParam(name = "activityTsUserId", value = "活动用户id", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态：0:考前说明，1：答题中心，2：结果页", paramType = "query", required = true)
    })
    @RequestMapping(value="/updateActivityTsUserStatus",method = {RequestMethod.POST})
    public ReturnResult<String> updateActivityTsUserStatus(@NotBlank(message="项目编号不能为空") String projectCode,
                                                           @NotNull(message="活动用户id 不能为空") Long activityTsUserId,
                                                           @NotNull(message="状态") Integer status) throws Exception{
        this.valAllClose(projectCode);
        ActivityTsUser activityTsUser = new ActivityTsUser();
        activityTsUser.setActivityTsUserId(activityTsUserId);
        activityTsUser.setStatus(status);
        activityTsUserService.updateByPrimaryKeySelective(activityTsUser);

        return ReturnResult.success("OK");
    }

    @ApiOperation(value="用户试卷详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "项目编号", paramType = "query", required = true),
            @ApiImplicitParam(name = "activityTsUserId", value = "活动用户id", paramType = "query", required = true)
    })
    @RequestMapping(value="/examDetailOfUser",method = {RequestMethod.POST})
    public ReturnResult<RespExamDetail> examDetailOfUser(@NotBlank(message="项目编号不能为空") String projectCode,
                                                         @NotNull(message="活动用户id 不能为空") Long activityTsUserId) throws Exception{
        this.valAllClose(projectCode);
        ResBoolSimpleInfo<RespExamDetail> res = activityTsUserService.examDetailOfUser(activityTsUserId);
        return ReturnResult.returnRes(res);
    }

    @ApiOperation(value="提交答案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "项目编号", paramType = "query", required = true),
            @ApiImplicitParam(name = "activityTsUserDetailId", value = "答题详情id", paramType = "query", required = true),
            @ApiImplicitParam(name = "answer(A、B、C、D)", value = "答案", paramType = "query", required = true)

    })
    @RequestMapping(value="/submitAnswer",method = {RequestMethod.POST})
    public ReturnResult<String> submitAnswer(@NotBlank(message="项目编号不能为空") String projectCode,
                                             @NotNull(message="答题详情id 不能为空") Long activityTsUserDetailId,
                                             @NotBlank(message="答案不能为空") String answer) throws Exception{
        this.valAllClose(projectCode);
        activityTsUserDetailService.submitAnswer(activityTsUserDetailId, answer);
        return ReturnResult.success("OK");
    }


    @ApiOperation(value="更新用户剩余时间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "项目编号", paramType = "query", required = true),
            @ApiImplicitParam(name = "activityTsUserId", value = "活动用户id", paramType = "query", required = true),
            @ApiImplicitParam(name = "time", value = "剩余时间", paramType = "query", required = true)

    })
    @RequestMapping(value="/updateTimeOfUser",method = {RequestMethod.POST})
    public ReturnResult<ActivityTsUser> updateTimeOfUser(@NotBlank(message="项目编号不能为空") String projectCode,
                                                         @NotNull(message="用户 id 不能为空") Long activityTsUserId,
                                                         @NotNull(message="剩余时间") Integer time) throws Exception{
        this.valAllClose(projectCode);
        ActivityTsUser activityTsUser = new ActivityTsUser();
        activityTsUser.setActivityTsUserId(activityTsUserId);
        activityTsUser.setTime(time >= 0 ? time : 0);
        activityTsUserService.updateByPrimaryKeySelective(activityTsUser);
        return ReturnResult.success(activityTsUser);
    }

    @ApiOperation(value="诊断结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "项目编号", paramType = "query", required = true),
            @ApiImplicitParam(name = "activityTsUserId", value = "活动用户id", paramType = "query", required = true)
    })
    @RequestMapping(value="/examResult",method = {RequestMethod.POST})
    public ReturnResult<RespExamResult> examResult(@NotBlank(message="项目编号不能为空") String projectCode,
                                                   @NotNull(message="活动用户id 不能为空") Long activityTsUserId) throws Exception{
        this.valAllClose(projectCode);
        RespExamResult respExamResult = activityTsUserDetailService.examResult(activityTsUserId);
        return ReturnResult.success(respExamResult);
    }



}
